package com.example.newinfinity.View.ColorPicker

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.newinfinity.MyViewModel.DrawViewModel
import com.example.newinfinity.R
import com.example.newinfinity.databinding.ColorAddItemBinding
import com.example.newinfinity.databinding.ColorDeleteItemBinding
import com.example.newinfinity.databinding.ColorPickerItemBinding

//调色盘弹窗下方的默认提供色块recyclerView
class ColorPickerAdapter : RecyclerView.Adapter<ColorPickerAdapter.ColorPickerViewHolder>() {
    private var colorList: List<Int> = arrayListOf()
    var addDeleteColorCallback: (Int) -> Unit = {}

    inner class ColorPickerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        fun bind(color: Int, type: Int) {
            when (type) {
                //0表示➕ 1表示➖
                0 -> {
                    itemView.setOnClickListener {
                        addDeleteColorCallback(0)
                    }
                }
                1 -> {
                    itemView.setOnClickListener {
                        addDeleteColorCallback(1)
                    }
                }
                2 -> {
                    //找到recyclerView中的item并配置颜色
                    val item = itemView.findViewById<View>(R.id.color_picker_item)
                    item.setBackgroundColor(color)
                    //点击某个颜色方块 就刷新全局绘制的画笔颜色
                    item.setOnClickListener {
                        DrawViewModel.getDrawViewModel().mColor = color
                    }
                }
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ColorPickerViewHolder {
        return when (viewType) {
            0 -> {
                val colorAddItemBinding =
                    ColorAddItemBinding.inflate(LayoutInflater.from(parent.context))
                ColorPickerViewHolder(colorAddItemBinding.root)
            }

            1 -> {
                val colorDeleteItemBinding =
                    ColorDeleteItemBinding.inflate(LayoutInflater.from(parent.context))
                ColorPickerViewHolder(colorDeleteItemBinding.root)
            }

            else -> {
                val colorPickerItemBinding =
                    ColorPickerItemBinding.inflate(LayoutInflater.from(parent.context))
                ColorPickerViewHolder(colorPickerItemBinding.root)
            }
        }
    }

    override fun getItemCount(): Int {
        //除了色块还有➕➖item需要添加上去
        return colorList.size + 2
    }

    override fun onBindViewHolder(holder: ColorPickerViewHolder, position: Int) {
        //前两个为➕➖，并不需要配置绑定颜色
        if (position == 0) {
            holder.bind(0, 0)
        }
        if (position == 1) {
            holder.bind(1, 1)
        }
        if (position > 1) {
            //position中的数量一共有colorNum+2个，所以仅仅绑定色块的数量需要减2
            val itemColor = colorList[position - 2]
            holder.bind(itemColor, 2)
        }
    }

    override fun getItemViewType(position: Int): Int {
        return when (position) {
            0 -> 0  //第一个加号返回0
            1 -> 1  //第二个减号返回1
            else -> 2   //其余色块返回2
        }
    }

    //外部提供颜色数据
    fun setData(colors: MutableList<Int>) {
        colorList = colors
        //刷新
        notifyDataSetChanged()
    }

}